from main_ui1 import Ui_Form as Main_Form_UI_1
from main_ui2 import Ui_Form as Main_Form_UI_2
from main_ui3 import  Ui_Form as Main_Form_UI_3
from main_ui import  Ui_Form as Main_Form_UI
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QDockWidget, QListWidget
from PyQt5.QtWidgets import QWidget
from PyQt5.Qt import *
import sys
sys.path.append("swig_target/")
import maincpp

class Main_Widget(QtWidgets.QWidget):
    def __init__(self, Main_Form_UI_X):
        self.widget = QtWidgets.QWidget()
        Main_Form_UI_X.setupUi(self.widget)

class Main_Form(QtWidgets.QWidget, Main_Form_UI):
    switch_window = QtCore.pyqtSignal()

    def __init__(self):
        super(Main_Form, self).__init__()
        self.window = QMainWindow()
        self.setupUi(self.window)
        self.pushButton_1.clicked.connect(self.show_panel)
        self.pushButton_2.clicked.connect(self.show_panel)
        self.pushButton_3.clicked.connect(self.show_panel)
        self.qsl = QStackedLayout(self.frame)
        self.UI1 = Main_Form_UI_1()
        self.UI2 = Main_Form_UI_2()
        self.UI3 = Main_Form_UI_3()
        self.qsl.addWidget(Main_Widget(self.UI1).widget)
        self.qsl.addWidget(Main_Widget(self.UI2).widget)
        self.qsl.addWidget(Main_Widget(self.UI3).widget)
        self.UI1.pushButton.clicked.connect(self.add_subject)
        self.UI1.pushButton_2.clicked.connect(self.delete_subject)
        self.UI2.pushButton.clicked.connect(self.add_file)
        self.UI2.pushButton_2.clicked.connect(self.delete_file)
        self.UI3.transferPushButton.clicked.connect(self.transfer_right)
        self.UI3.revokePushButton.clicked.connect(self.revoke_right)
        self.refresh_ui()

    def refresh_ui(self):
        self.UI1.tableWidget.clearContents();
        self.UI1.tableWidget.setRowCount(0)
        curRowCount = self.UI1.tableWidget.rowCount()
        num1 = maincpp.interface_get_subjects_num()
        for i in range(num1):
            name = maincpp.interface_get_subjects_name(i)
            passwd = maincpp.interface_get_subjects_passwd(i)
            self.UI1.tableWidget.setRowCount(curRowCount + 1)
            self.UI1.tableWidget.setItem(curRowCount, 0, QTableWidgetItem(name))
            curRowCount += 1

        self.UI2.tableWidget.clearContents();
        self.UI2.tableWidget.setRowCount(0)            
        curRowCount = self.UI2.tableWidget.rowCount()
        num1 = maincpp.interface_get_objects_num()
        for i in range(num1):
            name = maincpp.interface_get_objects_filename(i)
            creator = maincpp.interface_get_objects_creator(i)
            content = maincpp.interface_get_objects_content(i)
            self.UI2.tableWidget.setRowCount(curRowCount + 1)
            self.UI2.tableWidget.setItem(curRowCount, 0, QTableWidgetItem(name))
            self.UI2.tableWidget.setItem(curRowCount, 1, QTableWidgetItem(creator))
            self.UI2.tableWidget.setItem(curRowCount, 2, QTableWidgetItem(content))
            curRowCount += 1

        self.UI3.tableWidget.clearContents();
        self.UI3.tableWidget.setRowCount(0)            
        curRowCount = self.UI3.tableWidget.rowCount()
        num1 = maincpp.interface_get_right_num()
        d = {1: "READ", 2: "WRITE", 4:"OWN"}
        for i in range(num1):
            name = maincpp.interface_get_right_username(i)
            file = maincpp.interface_get_right_filename(i)
            from_to = maincpp.interface_get_right_from(i)
            right = maincpp.interface_get_right_access(i)
            self.UI3.tableWidget.setRowCount(curRowCount + 1)
            self.UI3.tableWidget.setItem(curRowCount, 0, QTableWidgetItem(name))
            self.UI3.tableWidget.setItem(curRowCount, 1, QTableWidgetItem(file))
            self.UI3.tableWidget.setItem(curRowCount, 2, QTableWidgetItem(d[right]))
            self.UI3.tableWidget.setItem(curRowCount, 3, QTableWidgetItem(from_to))
            curRowCount += 1

    def transfer_right(self):
        username = self.UI3.usernameLineEdit.text()
        filename = self.UI3.filenameLineEdit.text()
        right = self.UI3.rightLineEdit.text()
        if maincpp.interface_transfer_right(username, filename, right):
            self.refresh_ui()    
        else:
            reply = QMessageBox.warning(self, "warning", "transfer failed for some reason")

    def revoke_right(self):
        username = self.UI3.usernameLineEdit.text()
        filename = self.UI3.filenameLineEdit.text()
        right = self.UI3.rightLineEdit.text()
        if maincpp.interface_revoke_right(username, filename, right):
            self.refresh_ui()
        else:
            reply = QMessageBox.warning(self, "warning", "transfer failed for some reason")

    def add_subject(self):
        username = self.UI1.usernameLineEdit.text()
        passwd = self.UI1.passwdLineEdit.text()
        if maincpp.interface_add_user(username, passwd):
            self.refresh_ui()
        else:
            reply = QMessageBox.warning(self, "warning", "You are not allowed to add user")

    def delete_subject(self):
        username = self.UI1.usernameLineEdit.text()
        passwd = self.UI1.passwdLineEdit.text()
        if maincpp.interface_delete_user(username):
            self.refresh_ui()
        else:
            reply = QMessageBox.warning(self, "warning", "You are not allowed to delete user")

    def add_file(self):
        filename = self.UI2.filenameLineEdit.text()
        content = self.UI2.contentEdit.toPlainText()
        if maincpp.interface_add_file(filename, content):
            self.refresh_ui()
        else:
            reply = QMessageBox.warning(self, "warning", "create file failed for some reason")

    def delete_file(self):
        filename = self.UI2.filenameLineEdit.text()
        if maincpp.interface_delete_file(filename):
            self.refresh_ui()
        else:
            reply = QMessageBox.warning(self, "warning", "You are not allowed to delete user")

    def show_panel(self):
        dic = {"pushButton_1": 0, "pushButton_2": 1, "pushButton_3": 2};
        index = dic[self.sender().objectName()]
        self.qsl.setCurrentIndex(index)

    def show(self):
        self.window.show()

    def close(self):
        self.window.close()
